package ngram_converter;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.SwingWorker;
import main.Main;
import ngram.util.GuiUtil;
import ngram_converter.Converter.FileExistsOption;
import swingExtended.preferences.PreferenceGroup;
import swingExtended.progressPanel.ProgressPanel;
import swingExtended.progressPanel.ProgressPanelListener;
import swingExtended.progressPanel.ProgressPanelWorkerFactory;

/**
 * ConverterWindow.java
 *
 * Created on 12-Jan-2013, 8:01:47 PM
 * @author Shimu
 */
public class ConverterWindow extends javax.swing.JFrame {

    /** Creates new form ConverterWindow */
    public ConverterWindow() {
        initComponents();

        this.setLocationRelativeTo(this.getParent());

        // Get the preferences
        final PreferenceGroup PREF_GROUP = new PreferenceGroup(Preferences.userRoot().node(
                Main.USER_PREF_ROOT_NODE + "/" + this.getClass()));

        // Initialize the input panels
        inputBrowserPanel.getFileChooser().setFileSelectionMode(
                JFileChooser.FILES_AND_DIRECTORIES);

        outputBrowserPanel.getFileChooser().setFileSelectionMode(
                JFileChooser.FILES_AND_DIRECTORIES);

        //this.outputBrowserPanel.getFileChooser().setMultiSelectionEnabled(true);

        // Initialize the dropdown menus
        GuiUtil.addEnumAsOptions(FileExistsOption.class, dropDownFileExists);

        PREF_GROUP.add(inputBrowserPanel, "input", "");
        PREF_GROUP.add(outputBrowserPanel, "output", "");
        PREF_GROUP.add(dropDownFileExists, "fileExists", 0);
        PREF_GROUP.loadPreferences();

        // Event handlers
        this.progressPanel.addListener(new ProgressPanelListener() {

            @Override
            public void onStart(ProgressPanel emitter) {
                GuiUtil.setEnabled(false,
                        dropDownFileExists, inputBrowserPanel, outputBrowserPanel);
            }

            @Override
            public void onStop(ProgressPanel emitter) {
                GuiUtil.setEnabled(true,
                        dropDownFileExists, inputBrowserPanel, outputBrowserPanel);
            }
        });

        this.progressPanel.addWorkerFactory(new ProgressPanelWorkerFactory() {

            @Override
            public SwingWorker CreateWorker() {
                return new Converter_V3(progressPanel, inputBrowserPanel.getFile(), outputBrowserPanel.getFile(),
                        (FileExistsOption) dropDownFileExists.getSelectedItem());
            }
        });

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(WindowEvent e) {
                PREF_GROUP.savePreferences();
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputBrowserPanel = new swingExtended.inputBrowsePanel.InputBrowserPanel("Input Folder:");
        outputBrowserPanel = new swingExtended.inputBrowsePanel.InputBrowserPanel("Output Folder:");
        jLabel1 = new javax.swing.JLabel();
        progressPanel = new swingExtended.progressPanel.ProgressPanel();
        dropDownFileExists = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("If a file already exists in the output folder:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(progressPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 793, Short.MAX_VALUE)
                    .addComponent(outputBrowserPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 793, Short.MAX_VALUE)
                    .addComponent(inputBrowserPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 793, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addComponent(dropDownFileExists, 0, 589, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(inputBrowserPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputBrowserPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(dropDownFileExists, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox dropDownFileExists;
    private swingExtended.inputBrowsePanel.InputBrowserPanel inputBrowserPanel;
    private javax.swing.JLabel jLabel1;
    private swingExtended.inputBrowsePanel.InputBrowserPanel outputBrowserPanel;
    private swingExtended.progressPanel.ProgressPanel progressPanel;
    // End of variables declaration//GEN-END:variables
}
